package proxypattern.impl;


import lombok.Getter;
import proxypattern.base.BaseState;
import proxypattern.entity.RemoteGumballMachine;

import java.rmi.RemoteException;

/**糖果机已售出一颗糖果状态
 * @author yaojianfeng
 */
public class SoldStateImpl implements BaseState {
    @Getter
    private String desc = "糖果机正在出货";
    private transient RemoteGumballMachine gumballMachine;

    /**
     * 我们通过构造器得到糖果机的引用,然后将它记录在实例变量中
     * @param gumballMachine 糖果机对象
     */
    public SoldStateImpl(RemoteGumballMachine gumballMachine) {
        this.gumballMachine = gumballMachine;
    }

    @Override
    public void insertQuarter() {
        System.out.println("糖果正在出货中,请勿重复投币");
    }

    @Override
    public void ejectQuarter() {
        System.out.println("你以为我是棒槌啊 曲柄已经转动 都开始出货了你还想退钱?");
    }

    @Override
    public void turnCrank() {

        System.out.println("呆瓜!转动一次就够了 再多转我也不会一次给你两颗糖果!");
    }

    /**
     * 这是售出糖果状态所对应的正常的行为逻辑
     */
    @Override
    public void dispense() {
        gumballMachine.releaseBall();
        try {
            if (gumballMachine.getCount()>0){
                gumballMachine.setState(gumballMachine.getNoQuarterState());
            }else {
                gumballMachine.setState(gumballMachine.getSoldOutState());
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}
